import { Injectable } from '@nestjs/common';
import Redis from 'ioredis';

@Injectable()
export class RedisService {
    private redisClient: Redis;

    constructor() {
        this.redisClient = new Redis({
            host: '127.0.0.1',
            port: 6379,
        });
    }

    // 通过get方法访问redis
    async get(key: string): Promise<string | null> {
        return await this.redisClient.get(key);
    }

    // 通过set方法存
    async set(key: string, value: string): Promise<void> {
        await this.redisClient.set(key, value);
    }

    // 关闭redis链接
    async onModuleDestroy() {
        await this.redisClient.quit();
    }
}
